clear
set matsize 10000
use "${data}IndirectSurveyDataReplication.dta", clear

estimates clear
keep if complete == 1

foreach x in  PIB IPW PCA {

egen `x'sd = sd(`x'_leader)
egen `x'mu = mean(`x'_leader)
gen z`x'_leader = (`x'_leader-`x'mu)/`x'sd
drop *sd *mu
egen `x'sd = sd(`x'_HH)
egen `x'mu = mean(`x'_HH)
gen z`x'_HH = (`x'_HH-`x'mu)/`x'sd


reg prop_asiste z`x'_leader if num_1215 > 0 |  num_1518 > 0, cluster(ejnclnmid)
estimates store `x'e1
sum    prop_asiste if e(sample)
estadd scalar mn_`x' =r(mean)


**** test equality of coefficients ******
reg prop_asiste z`x'_leader if num_1215 > 0 |  num_1518 > 0
estimates store t`x'e1
reg prop_asiste z`x'_HH if e(sample)
estimates store t`x'e2
suest t`x'e1 t`x'e2, cluster(ejnclnmid)
test [t`x'e1_mean]z`x'_leader - [t`x'e2_mean]z`x'_HH =0
scalar p`x' = r(p)
reg prop_asiste z`x'_HH if e(sample), cluster(ejnclnmid)
estimates store `x'e2
estadd scalar p`x'
sum    prop_asiste if e(sample)
estadd scalar mn_`x' =r(mean)



reg hh_migracion z`x'_leader, cluster(ejnclnmid)
estimates store `x'e3
sum    hh_migracion if e(sample)
estadd scalar mn_`x' =r(mean)


**** test equality of coefficients ******
reg hh_migracion z`x'_leader 
estimates store t`x'e1
reg hh_migracion z`x'_HH if e(sample)
estimates store t`x'e2
suest t`x'e1 t`x'e2, cluster(ejnclnmid)
test [t`x'e1_mean]z`x'_leader - [t`x'e2_mean]z`x'_HH =0
scalar p`x' = r(p)
reg hh_migracion  z`x'_HH if e(sample), cluster(ejnclnmid)
estimates store `x'e4
estadd scalar p`x'
sum    hh_migracion if e(sample)
estadd scalar mn_`x' =r(mean)

xtreg prop_asiste z`x'_leader if num_1215 > 0 |  num_1518 > 0, fe i(ejnclnmid) cluster(ejnclnmid)
estimates store `x'e5
sum    prop_asiste if e(sample)
estadd scalar mn_`x' =r(mean)


**** test equality of coefficients ******
quietly: reg prop_asiste z`x'_leader i.ejnclnmid if num_1215 > 0 |  num_1518 > 0
estimates store t`x'e1
quietly: reg prop_asiste z`x'_HH i.ejnclnmid if e(sample)
estimates store t`x'e2
suest t`x'e1 t`x'e2, cluster(ejnclnmid)
test [t`x'e1_mean]z`x'_leader - [t`x'e2_mean]z`x'_HH =0
scalar p`x' = r(p)
xtreg prop_asiste z`x'_HH if e(sample), fe i(ejnclnmid) cluster(ejnclnmid)
estimates store `x'e6
estadd scalar p`x'

sum    prop_asiste if e(sample)
estadd scalar mn_`x' =r(mean)

xtreg hh_migracion z`x'_leader, fe i(ejnclnmid) cluster(ejnclnmid)
estimates store `x'e7
sum    hh_migracion if e(sample)
estadd scalar mn_`x' =r(mean)


**** test equality of coefficients ******
quietly: reg hh_migracion z`x'_leader i.ejnclnmid
estimates store t`x'e1
quietly: reg hh_migracion z`x'_HH i.ejnclnmid if e(sample)
estimates store t`x'e2
suest t`x'e1 t`x'e2, cluster(ejnclnmid)
test [t`x'e1_mean]z`x'_leader - [t`x'e2_mean]z`x'_HH =0
scalar p`x' = r(p)
xtreg hh_migracion  z`x'_HH if e(sample), fe i(ejnclnmid) cluster(ejnclnmid)
estimates store `x'e8
estadd scalar p`x'
sum    hh_migracion if e(sample)
estadd scalar mn_`x' =r(mean)


}

esttab PIBe*  ///
	using "${outputs}regressions.tex", replace fragment ///
	keep(zPIB*) ///
	se nonotes  style(tex)  b(%12.3f) se(%12.3f) scalars("pPIB P-value diff.") noobs ///
	starlevels(* 0.10 ** 0.05 *** 0.01) label mlabels("" "" "" "" "" "" "" ""  ) ///
	coeflabels( zIPW_HH "IPW index, hh responses" zPIB_HH "Simple index, hh responses" zPCA_HH "PCA index, hh responses" ///
	zIPW_leader "IPW index, leader responses" zPIB_leader "Simple index, leader responses" zPCA_leader "PCA index, leader responses") ///
	 prehead({ \begin{tabular}{lcccccccc}    ///
	 \hline \hline ///
	 & \multicolumn{2}{c}{Secondary} & \multicolumn{2}{c}{Migration} &   \multicolumn{2}{c}{Secondary} & \multicolumn{2}{c}{Migration} \\   ) ///
	posthead(\hline A: Simple index & \\ )	  ///
	

esttab IPW*  ///
	using "${outputs}regressions.tex",  ///
	se nonotes  style(tex)  b(%12.3f) se(%12.3f) scalars("pIPW P-value diff.")  noobs ///
	starlevels(* 0.10 ** 0.05 *** 0.01) label mlabels("" "" "" "" "" "" "" "" ) ///
	coeflabels( zIPW_HH "IPW index, hh responses" zPIB_HH "Simple index, hh responses" zPCA_HH "PCA index, hh responses" ///
	zIPW_leader "IPW index, leader responses" zPIB_leader "Simple index, leader responses" zPCA_leader "PCA index, leader responses") ///
	 keep(zIPW*)  nonumbers  append  fragment ///
	 posthead(\hline B: IPW index & \\  [1em]) 
	

esttab PCA* ///
	using "${outputs}regressions.tex", append ///
	keep( zPCA*) ///
	se nonotes  style(tex) fragment b(%12.3f) se(%12.3f) scalars("mn_PCA Mean DV" "N Obs"  "pPCA P-value diff.")  nonumbers ///
	starlevels(* 0.10 ** 0.05 *** 0.01) label mlabels("" "" "" "" "" "" "" "") ///
coeflabels( zIPW_HH "IPW index, hh responses" zPIB_HH "Simple index, hh responses" zPCA_HH "PCA index, hh responses" ///
	zIPW_leader "IPW index, leader responses" zPIB_leader "Simple index, leader responses" zPCA_leader "PCA index, leader responses") ///
	 posthead(\hline C: PCA index &  \\ [1em]) ///
	 prefoot( \hline \hline  Ejido FE & no & no & no & no & yes & yes & yes & yes \\) ///
	postfoot(\hline \hline \end{tabular} } \begin{tablenotes}[para,flushleft] \footnotesize{Estimate shown are coefficients from OLS and FE ///
	OLS regressions of the proportions of children in a household attending secondary school or higher, or on having an international migrant ///
	on the binary wealth indices.  ///
	School age is defined as 12-18 years old. Regressions only use households who have children in one ///
	or both of these age categories.  Standard errors are robust and clustered at the ejidal level.  Only households with complete information for both leader and household ///
	indices are used in the regressions. Indices are normalized before using in regression. The p-value in the last row of each panel ///
	is a chi-squared test for the difference in coefficients between the leader and the household reported measure. ///
	* p $<$.10, ** p$<$ .05, *** p$<$.01. } \end{tablenotes} )
	
